package com.learning.jquizenglish.screen.profile;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.learning.jquizenglish.JQuizEnglishApp;
import com.learning.jquizenglish.model.ProfileModel;

public class ProfilePreference {

	private static ProfilePreference INSTANCE = null;
	/** The Constant PREF_KEY. */
	private static final String PREF_KEY = "_profile_key";
	
	/** The Constant FILE_NAME. */
	private static final String FILE_NAME = "_profile_data";
	
	private static final String AVATAR_PATH_KEY = "_avatar_path_key";
	
	private static final String NOTE_KEY = "_note_key";
	
	/** The m prefs. */
	private SharedPreferences  mPrefs;
	
	private ProfileModel profileModel;
	
	private ProfilePreference(){
		mPrefs = JQuizEnglishApp.getAppContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		retrieveData();
	}
	
	public static ProfilePreference getInstance(){
		if (INSTANCE == null) {
			INSTANCE = new ProfilePreference();
		}
		return INSTANCE;
	}
	
	public void saveChange(){
//		 if(profileModel == null) return;
//		 Editor prefsEditor = mPrefs.edit();
//	     Gson gson = new Gson();
//	     String json = gson.toJson(profileModel);
//	     if(json == null) return;
//	     prefsEditor.remove(PREF_KEY);
//	     prefsEditor.putString(PREF_KEY, json);
//	     prefsEditor.commit();
	}
	
	public ProfileModel getProfile(){
		return profileModel;
	}
	
	public void setProfile(ProfileModel model){
		profileModel = model;
	}
	
	public void saveAvatarUriToPref(String path){
		Editor prefsEditor = mPrefs.edit();
		prefsEditor.remove(AVATAR_PATH_KEY);
	    prefsEditor.putString(AVATAR_PATH_KEY, path);
	    prefsEditor.commit();
	}
	
	public String getAvatarUriFromPref(){
		return mPrefs.getString(AVATAR_PATH_KEY, "");
	}
	
	public void saveUserNote(String note){
		Editor prefsEditor = mPrefs.edit();
		prefsEditor.remove(NOTE_KEY);
	    prefsEditor.putString(NOTE_KEY, note);
	    prefsEditor.commit();
	}
	
	public String getNoteContent(){
		return mPrefs.getString(NOTE_KEY, "");
	}
	
	public void retrieveData(){
//		String json = mPrefs.getString(PREF_KEY, "");
//		Gson gson = new Gson();
//		profileModel = gson.fromJson(json, ProfileModel.class);
	}
}
